{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/python3\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "import os\n",
    "import sys\n",
    "import time\n",
    "import math\n",
    "import pandas as pd\n",
    "import numpy  as np\n",
    "from datetime import datetime, timedelta\n",
    "import traceback\n",
    "from utils.timecheck import ntp_client\n",
    "import json\n",
    "\n",
    "from api.asset     import cal_strategy_trade_usdt\n",
    "from api.position  import update_symbol_info, reset_leverage\n",
    "from api.market    import fetch_all_binance_swap_candle_data, fetch_binance_ticker_data\n",
    "from api.trade     import cal_order_amount, place_order, get_twap_symbol_info_list\n",
    "from utils.notification import send_msg_for_position, send_wechat_work_msg,send_dataframe\n",
    "\n",
    "from functions     import cal_factor, set_fundingrate,Select_coin\n",
    "from utils.commons import robust, sleep_until_run_time\n",
    "from rebalance.fee import replenish_bnb\n",
    "from config        import *\n",
    "\n",
    "sys.stdout.reconfigure(encoding='utf-8')\n",
    "\n",
    "def retry_wrapper(func, params={}, func_name='', retry_times=5, sleep_seconds=5, if_exit=True):\n",
    "    for _ in range(retry_times):\n",
    "        try:\n",
    "            result = func(params=params)\n",
    "            return result\n",
    "        except Exception as e:\n",
    "            print(func_name, '报错，报错内容：', str(e), '程序暂停(秒)：', sleep_seconds)\n",
    "            time.sleep(sleep_seconds)\n",
    "    else:\n",
    "        if if_exit:\n",
    "            raise ValueError(func_name, '报错重试次数超过上限，程序退出。')\n",
    "\n",
    "def run():\n",
    "    # ntp_client()\n",
    "    # ====初始化\n",
    "    quant.initialize()\n",
    "    debug = quant.debug\n",
    "    # ===设置默认最大杠杆数，变相增加资金容量\n",
    "\n",
    "\n",
    "    #获取全策略的列表\n",
    "    all_strategy_list = []\n",
    "    for strategy_ in strategy_dic:\n",
    "        all_strategy_list += strategy_dic[strategy_]['strategy_list']\n",
    "\n",
    "    # ===加载市场信息\n",
    "    quant.load_market()\n",
    "    # =====设置参数\n",
    "    exchange        \t = quant.exchange\n",
    "    symbol_list     \t = quant.symbol_list\n",
    "    min_qty         \t = quant.min_qty\n",
    "    price_precision \t = quant.price_precision\n",
    "    min_notional  \t\t = quant.min_notional\n",
    "    njob1           \t = quant.njob1\n",
    "    njob2           \t = quant.njob2\n",
    "    trade_ratio     \t = quant.trade_ratio\n",
    "    strategy_list   \t = all_strategy_list\n",
    "    min_kline_size  \t = quant.min_kline_size\n",
    "    max_one_order_amount = quant.max_one_order_amount\n",
    "    twap_interval        = quant.twap_interval\n",
    "\n",
    "    for strategy_ in strategy_dic:\n",
    "        apiKey = strategy_dic[strategy_]['apiKey']\n",
    "        secret = strategy_dic[strategy_]['secret']\n",
    "        strategy_list = strategy_dic[strategy_]['strategy_list']\n",
    "        trade_ratio = strategy_dic[strategy_]['trade_ratio']\n",
    "        quant_ = QuantConfig(apiKey, secret, proxy, njob1, njob2, trade_ratio, min_kline_size, black_list,\n",
    "                        max_one_order_amount, twap_interval, strategy_list, debug=Debug)\n",
    "        quant_.initialize()\n",
    "        if not debug:\n",
    "            reset_leverage(quant_.exchange, max_leverage=5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38trading",
   "language": "python",
   "name": "py38trading"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
